package com.example.java_chatroom.Controller;

import com.example.java_chatroom.model.FriendInfo;
import com.example.java_chatroom.model.Result;
import com.example.java_chatroom.model.UserInfo;
import com.example.java_chatroom.service.FriendInfoService;
import com.example.java_chatroom.service.MessageService;
import com.example.java_chatroom.service.MessageSessionService;
import com.example.java_chatroom.service.UserInfoService;
import com.example.java_chatroom.utils.GetUserInfoUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/friendInfo")
public class FriendInfoController {
    @Autowired
    private FriendInfoService friendInfoService;
    @Autowired
    private MessageSessionService messageSessionService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private UserInfoService userInfoService;
    private ObjectMapper objectMapper = new ObjectMapper();

    @RequestMapping("/IsYourFriend")
    public Result IsYourFriend(Integer friendId, HttpServletRequest request) {
        UserInfo userInfo = GetUserInfoUtils.getUserInfo(request);
        if (userInfo == null) {
            return Result.fail("用户未登陆");
        }
        Integer i = friendInfoService.queryFriendByFriendAndUserId(friendId, userInfo.getUserId());
        if (i == null) {
            //1表示不是好友
            return Result.success(1);
        }else {
            //2表示是好友
            return Result.success(2);
        }
    }
    @RequestMapping("/getFriendByFriendId")
    public Result getFriendByFriendId(Integer friendId) {
        String userName = userInfoService.queryUserNameById(friendId);
        return Result.success(userName);
    }


    @RequestMapping("/deleteFriend")
    public Result deleteFriend(Integer friendId, HttpServletRequest request) {
        UserInfo userInfo = GetUserInfoUtils.getUserInfo(request);
        if (userInfo == null) {
            return Result.fail("用户不存在");
        }
        //删除好友
        friendInfoService.deleteFriendByUserIdAndfriendId(friendId, userInfo.getUserId());
        List<Integer> list = messageSessionService.queryUserSessionIdByuserId(userInfo.getUserId());
        for(Integer sessionId :list) {
            List<Integer> list1 = messageSessionService.queryUserIdBysessionId(sessionId);
            System.out.println(list1.toString());
            if (list1.size() == 2) {
                for (Integer friendId1 : list1)
                 if (friendId1 == friendId) {
                     //删除好友会话  会话信息 会话内容
                        messageSessionService.deleteSessionUser(sessionId);
                        messageSessionService.deleteSession(sessionId);
                        messageService.deleteMessageBysessionId(sessionId);
                    }

            }
        }
        return Result.success(1);
        }
    @RequestMapping("/getOtherFriendList")
    public Result getOtherFriendList(String friends,HttpServletRequest request) {
        List<FriendInfo> friendInfos = null;
        try {
             friendInfos = objectMapper.readValue(friends, new TypeReference<List<FriendInfo>>() {
            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        UserInfo userInfo = GetUserInfoUtils.getUserInfo(request);
        if (userInfo == null) {
            return Result.fail(null);
        }
        List<FriendInfo> friendInfos1 = friendInfoService.getOtherFriendList(userInfo, friendInfos);
        if (friendInfos1 == null) {
            return Result.fail("您的好友都在这个群里面了");
        }
        return Result.success(friendInfos1);
    }
    @RequestMapping("/getFriend")
    public Result getFriend(Integer sessionId) {

        List<FriendInfo> friendInfos = friendInfoService.getGroupList(sessionId);
        return Result.success(friendInfos);
    }

    @RequestMapping("/getFriendList")
    public Result getFriendList(HttpServletRequest request) {

        //先从会话中获取userId是啥
        HttpSession session = request.getSession(false);
        if (session == null) {
            //当前用户会话不存在 未登陆
            System.out.println("[getFriendList] 当前获取不到 Session 对象！");
            return Result.fail("用户未登陆");
        }
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        if (userInfo == null) {
            //当前用户对象没在会话中存在
            return Result.fail("没有找到UserInfo对象");
        }
        //根据userId从数据库查询数据
        List<FriendInfo> lists = friendInfoService.getFriendList(userInfo.getUserId());

        return Result.success(lists);
    }
}
